/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


// -- define namespace;
if (typeof HCM !== 'object') var HCM = {};

HCM.navigation = new (function() {
    var $root_el;
    var $main_menu_root;
    var $sub_menu_root;
    var current_selected_menu_item_el;
    var current_selected_sub_menu_el;
    var nav_search_input_el;

    this.onReady = function(el) {
        $root_el = $(el);
        $main_menu_root = $(el).find('#mainMenu');
        $sub_menu_root = $(el).find('.submenu');

        current_selected_menu_item_el = $main_menu_root.find('li.active').get(0);
        current_selected_sub_menu_el = $sub_menu_root.find('li.active').get(0);
        nav_search_input_el = $root_el.find('.navSearch input');

//        $sub_menu_root.mouseleave(function(){
//            $sub_menu_root.removeClass('activeSubmenu');
//        });

        $main_menu_root.find('li').click(function() {
            if (this != current_selected_menu_item_el) {
                $(this).addClass('active');
                $(current_selected_menu_item_el).removeClass('active');
                hideSubmenu($(current_selected_menu_item_el).find('a').attr('href'));
                current_selected_menu_item_el = this;
                showSubmenu($(this).find('a').attr('href'));
            } else {
                $(current_selected_menu_item_el).removeClass('active');
                hideSubmenu($(current_selected_menu_item_el).find('a').attr('href'));
                current_selected_menu_item_el = null;
            }
            return false;
        });

        $sub_menu_root.find('li').mouseover(function() {
            if (this != current_selected_sub_menu_el) {
                $(this).addClass('active');
                $(current_selected_sub_menu_el).removeClass('active');
                current_selected_sub_menu_el = this;
            }
            return false;
        });

        nav_search_input_el.focus(function(){
            $(this).val('');
        });

        nav_search_input_el.blur(function(){
            if ($.trim($(this).val()) == '') {
                $(this).val('Search');
            }
        });
    };

    // id is with #
    var showSubmenu = function (id) {
        var sub_menu=$('div'+id);
        sub_menu.addClass('activeSubmenu');
    };

    var hideSubmenu = function (id) {
        $('div'+id).removeClass('activeSubmenu');
    };


});

$(function() {
    HCM.navigation.onReady($("div#navigation"));
});